package com.zhuiyun.project.api.memberTypeManagement.service.impl;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuiyun.project.api.memberTypeManagement.entity.MemberTypeManagement;
import com.zhuiyun.project.api.memberTypeManagement.mapper.MemberTypeMapper;
import com.zhuiyun.project.api.memberTypeManagement.model.MemberTypeIdModel;
import com.zhuiyun.project.api.memberTypeManagement.model.MemberTypeManagementModel;
import com.zhuiyun.project.api.memberTypeManagement.model.MemberTypeModel;
import com.zhuiyun.project.api.memberTypeManagement.service.MemberTypeService;
import com.zhuiyun.project.api.memberUserManagement.mapper.MemberUserManagementMapper;
import com.zhuiyun.project.api.system.user.entity.SysUser;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.BeanUtils;
import com.zhuiyun.project.utils.StringUtils;
import com.zhuiyun.project.utils.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import com.zhuiyun.project.utils.pageUtil.PageResult;
import com.zhuiyun.project.utils.validator.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
/**
 * @ClassName MemberTypeServiceImpl
 * @Description TODO 会员管理接口实现类
 * @Author WX
 * @Date 2023/11/2 11:39
 **/
@Service
public class MemberTypeServiceImpl extends ServiceImpl<MemberTypeMapper,MemberTypeManagement> implements MemberTypeService {
    @Autowired
    private MemberTypeMapper memberTypeMapper;
    @Autowired
    private MemberUserManagementMapper memberUserManagementMapper;
    /**
     * @Author WX
     * @Description 获取会员类型信息
     * @Date 2023/11/2 13:52
     * @Return
     * @param pageRequest memberTypeManagementModel
     */
    @Override
    public PageResult getMemberType(PageRequest pageRequest, MemberTypeManagementModel memberTypeManagementModel) {
        MybatisPageHelper.startPage(pageRequest);
        List<MemberTypeManagement> MemberList = memberTypeMapper.getMemberType(memberTypeManagementModel);
        PageResult pageResult = MybatisPageHelper.getPageResult(MemberList);
        return pageResult;
    }
    /**
     * @Author WX
     * @Description 通过id获取会员类型信息
     * @Date 2023/11/2 15:20
     * @Return
     * @param id
     */
    @Override
    public MemberTypeManagement getById(Integer id, String venueUniqueIdentifier) {
        // 判断参数是否为空
        if (id == null || id <= 0 || venueUniqueIdentifier == null || venueUniqueIdentifier.isEmpty()) {
            throw new IllegalArgumentException("参数不能为空或者小于等于0");
        }
        MemberTypeManagement memberType = memberTypeMapper.getById(id, venueUniqueIdentifier);
        // 判断返回数据是否为空
        if (memberType == null) {
            throw new IllegalStateException("返回数据为空");
        }
        return memberType;
    }
    /**
     * @Author WX
     * @Description 添加会员类型信息
     * @Date 2023/11/2 15:58
     * @Return
     * @param memberTypeModel
     */
    @Override
    public CommonResult addMember(MemberTypeModel memberTypeModel) throws CommonException {
        if (ValidatorUtil.ValidationObject(memberTypeModel)){
            MemberTypeManagement memberTypeManagement = new MemberTypeManagement();
            BeanUtils.copy(memberTypeModel,memberTypeManagement);
            memberTypeManagement.setCreateTime(new Date());
            memberTypeManagement.setUpdateTime(new Date());
            memberTypeManagement.setEnableStatus(1);
            memberTypeManagement.setDelFlag(0);
//            boolean i = memberTypeService.save(memberTypeManagement);
            int i = memberTypeMapper.addMember(memberTypeManagement);
            if (i>0){
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            }else {
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        }else {
            return CommonResult.ok(EmErrorCode.SAVE_ERROR);
        }
    }
    /**
     * @Author WX
     * @Description 通过id删除会员类型信息
     * @Date 2023/11/2 19:02
     * @Return
     * @param id
     */
    @Override
    public CommonResult delMember(Integer id,String venueUniqueIdentifier) {
        if(StringUtils.isNotNull(id) && StringUtils.isNotNull(venueUniqueIdentifier)){
            int i = memberTypeMapper.delMember(id,venueUniqueIdentifier);
            if(i>0){
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            }else {
                return CommonResult.ok(EmErrorCode.DELETE_ERROR);
            }
        }else {
            return CommonResult.error(EmErrorCode.OBJECT_ISNULL);
        }
    }
    /**
     * @Author WX
     * @Description 通过id修改会员类型信息
     * @Date 2023/11/2 19:19
     * @Return
     * @param memberTypeModel
     */
    @Override
    public CommonResult updateMemberById(MemberTypeModel memberTypeModel) throws CommonException {
        if (ValidatorUtil.ValidationObject(memberTypeModel)){
            MemberTypeManagement memberTypeManagement = new MemberTypeManagement();
            BeanUtils.copy(memberTypeModel,memberTypeManagement);
            memberTypeManagement.setUpdateTime(new Date());
            memberTypeManagement.setEnableStatus(1);
            memberTypeManagement.setDelFlag(0);
//            boolean i = memberTypeService.save(memberTypeManagement);
            int i = memberTypeMapper.updateMemberById(memberTypeManagement);
            if (i>0){
                return CommonResult.ok(EmErrorCode.UPDATE_OK);
            }else {
                return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
            }
        }else {
            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
        }
    }
    /**
     * @Author WX
     * @Description 修改会员状态
     * @Date 2023/11/2 19:32
     * @Return
     * @param memberTypeIdModel
     */
    @Override
    public CommonResult updateEnableStatus(MemberTypeIdModel memberTypeIdModel) {
        int id = memberTypeIdModel.getId();
        int enableStatu = memberTypeIdModel.getEnableStatus();
        String venueUniqueIdentifier = memberTypeIdModel.getVenueUniqueIdentifier();
        return CommonResult.ok(memberTypeMapper.updateEnableStatus(id,enableStatu,venueUniqueIdentifier));
    }
    /**
     * @Author wx
     * @Description 查看该会员类型下是否有会员数据
     * @Date 2023/12/7 9:54
     * @Return
     * @param memberTypeId
     */
    @Override
    public boolean getByMemberTypeId(Integer memberTypeId, String venueUniqueIdentifier) {
        if (StringUtils.isNotNull(memberTypeId) && StringUtils.isNotNull(venueUniqueIdentifier)){
            int result = memberUserManagementMapper.getByMemberTypeId(memberTypeId,venueUniqueIdentifier);
            return result > 0;
        }else {
            return false;
        }
    }
}
